//
//  ZClubTypeChoseVC.m
//  WeClubs
//
//  Created by Zed Link on 11/5/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZClubTypeChoseVC.h"
#import "ZPowerCell.h"
#import "ZCommentHeader.h"
#import "ZResourceManager.h"

NSString * const ClubTypeCellReuseID = @"ZClubTypeCellReuseID";
NSString * const ClubTypeHeaderReuseID = @"ZClubTypeHeaderReuseID";

@interface ZClubTypeChoseVC () <ZPowerCellDelegate>

/** 数据源 */
@property (strong, nonatomic) NSArray <NSDictionary *> *dataSource;

@end

@implementation ZClubTypeChoseVC

+ (instancetype)zNew
{
    return [self zNewGroup];
}

#pragma mark - About Navigation

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"权限选择";
}

#pragma mark - View Life Cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.contentInset = UIEdgeInsetsMake(16, 0, 0, 0);
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 52;
    self.tableView.separatorColor = SEPARATOR_BG_COLOR;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZPowerCell" bundle:nil] forCellReuseIdentifier:ClubTypeCellReuseID];
    [self.tableView registerClass:[ZCommentHeader class] forHeaderFooterViewReuseIdentifier:ClubTypeHeaderReuseID];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *data = self.dataSource[indexPath.row];
    ZPowerCell *cell = [tableView dequeueReusableCellWithIdentifier:ClubTypeCellReuseID];
    cell.delegate = self;
    [cell setPowerName:data[ClubTypeNameKey] andDescription:data[ClubTypeDescKey]];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZCommentHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ClubTypeHeaderReuseID];
    header.alignment = ZTableHeaderAlignmentCenter;
    header.style = ZTableHeaderStyleSetting;
    [header zSetTitle:@"权限选择"];
    
    return  header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SETTING_STYLE_HEIGHT;
}

#pragma mark - ZPowerCellDelegate

- (void)powerCell:(ZPowerCell *)cell selectPower:(BOOL)selected withDescription:(NSString *)powerDesc
{
    if ([self.delegate respondsToSelector:@selector(clubTypeChoseControllerdidSelectClubType:)]) {
        [self.delegate clubTypeChoseControllerdidSelectClubType:powerDesc];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Getter & Setter

- (NSArray<NSDictionary *> *)dataSource
{
    if (_dataSource == nil) {
        _dataSource = [ZResourceManager sharedInstance].clubType;
    }
    return _dataSource;
}

@end
